02 / 22

How is RTK Query different from createAsyncThunk?

Both RTK Query and createAsyncThunk handle asynchronous operations in Redux Toolkit, but they serve different purposes. While createAsyncThunk is a low-level utility for managing async logic manually within slices, RTK Query provides a complete abstraction layer for data fetching, caching, and synchronization.

Key Differences Between RTK Query and createAsyncThunk
  1. 1

    RTK Query automatically handles caching, invalidation, and background refetching; createAsyncThunk does not.

  2. 2

    createAsyncThunk requires manual handling of loading, success, and error states in reducers.

  3. 3

    RTK Query integrates directly with React through auto-generated hooks like useGetPostsQuery.

  4. 4

    RTK Query reduces boilerplate by managing API calls and normalization automatically.

  5. 5

    createAsyncThunk is more flexible for custom async logic outside typical API requests.

Example Comparison

In short, createAsyncThunk gives fine-grained control over async flows, while RTK Query offers a high-level, declarative way to manage server-state with minimal effort.